//
//  ChartsAPI.swift
//  TimeBoat
//
//  Created by bo wang on 2022/8/24.
//  Copyright © 2022 Time Boat. All rights reserved.
//

import Foundation

extension APIManager {
    /// 图表相关接口具有相同的请求参数
    func chartsCommonParams(_ date: Date, imei: String?) -> [String: Any] {
        var params = [String: Any]()
        params["date"] = serverDateStrFromDate(date)
        params["imei"] = imei
        params["token"] = AccountManager.shareAccount().token()
        params["userUUID"] = AccountManager.shareAccount().userUUID()
        return params
    }
}

extension APIManager {
    
    /// 查询步数数据，这里只查询 日、周、月数据，年数据有另外接口
    @discardableResult
    func getStepsData(_ date: Date,
                      _ granularity: DateGranularity,
                      imei: String?,
                      completion: APICompletion<StepsNormalDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        switch granularity {
        case .day:
            return POST("total-step/daily", params, completion)
        case .week:
            return POST("total-step/weekly", params, completion)
        case .month:
            return POST("total-step/monthly", params, completion)
        case .year:
            return nil
        }
    }
    
    /// 查询步数的年数据
    @discardableResult
    func getStepsYearlyData(_ date: Date,
                            imei: String?,
                            completion: APICompletion<StepsYearlyDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        return POST("total-step/yearly", params, completion)
    }
}

extension APIManager {
    /// 查询心率数据，这里只查询 日、周、月数据，年数据有另外接口
    @discardableResult
    func getHeartRateData(_ date: Date,
                          _ granularity: DateGranularity,
                          imei: String?,
                          completion: APICompletion<HeartRateNormalDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        switch granularity {
        case .day:
            return POST("heart-rate/daily", params, completion)
        case .week:
            return POST("heart-rate/weekly", params, completion)
        case .month:
            return POST("heart-rate/monthly", params, completion)
        case .year:
            return nil
        }
    }
    
    /// 查询心率的年数据
    @discardableResult
    func getHeartRateYearlyData(_ date: Date,
                            imei: String?,
                            completion: APICompletion<HeartRateYearlyDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        return POST("heart-rate/yearly", params, completion)
    }
}

extension APIManager {
    /// 查询血氧数据，这里只查询 日、周、月数据，年数据有另外接口
    @discardableResult
    func getBloodOxygenData(_ date: Date,
                            _ granularity: DateGranularity,
                            imei: String?,
                            completion: APICompletion<BloodOxygenNormalDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        switch granularity {
        case .day:
            return POST("blood-oxygen/daily", params, completion)
        case .week:
            return POST("blood-oxygen/weekly", params, completion)
        case .month:
            return POST("blood-oxygen/monthly", params, completion)
        case .year:
            return nil
        }
    }
    
    /// 查询血氧的年数据
    @discardableResult
    func getBloodOxygenYearlyData(_ date: Date,
                                  imei: String?,
                                  completion: APICompletion<BloodOxygenYearlyDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        return POST("blood-oxygen/yearly", params, completion)
    }
}

extension APIManager {
    /// 查询体温数据，这里只查询 日、周、月数据，年数据有另外接口
    @discardableResult
    func getTemperatureData(_ date: Date,
                            _ granularity: DateGranularity,
                            imei: String?,
                            completion: APICompletion<TemperatureNormalDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        switch granularity {
        case .day:
            return POST("body-temperature/daily", params, completion)
        case .week:
            return POST("body-temperature/weekly", params, completion)
        case .month:
            return POST("body-temperature/monthly", params, completion)
        case .year:
            return nil
        }
    }
    
    /// 查询体温的年数据
    @discardableResult
    func getTemperatureYearlyData(_ date: Date,
                                  imei: String?,
                                  completion: APICompletion<TemperatureYearlyDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        return POST("body-temperature/yearly", params, completion)
    }
}

extension APIManager {
    /// 查询所有运动数据。 granularity 只支持 周、月、年
    @discardableResult
    func getSportTotalData(_ date: Date,
                           _ granularity: DateGranularity,
                           imei: String?,
                           completion: APICompletion<SportTotalDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        switch granularity {
        case .day:
            return nil
        case .week:
            return POST("sport-data/weekly", params, completion)
        case .month:
            return POST("sport-data/monthly", params, completion)
        case .year:
            return POST("sport-data/yearly", params, completion)
        }
    }
    
    /// 具体运动类型的数据列表
    /// - Parameters:
    ///   - start: 数据开始时间
    ///   - end: 数据结束时间
    ///   - type: 运动类型
    ///   - imei: imei
    ///   - completion: 回调
    @discardableResult
    func getSportDetailData(_ start: Date,
                            end: Date,
                            type: Int,
                            imei: String?,
                            completion: APICompletion<SportDetailDataUnit>? = nil)
    -> APITASK? {
        var params: [String: Any] = chartsCommonParams(Date(), imei: imei)
        params["date"] = nil
//        params["beginDate"] = serverDateStrFromDate(start)
//        params["endDate"] = serverDateStrFromDate(end)
        params["beginDate"] = ChartsDataUtil.dateFormatter1.string(from: start)
        params["endDate"] = ChartsDataUtil.dateFormatter1.string(from: end)
        
        var api: String?
        switch type {
        case 33: // 羽毛球
            api = "badminton/list"
        case 34: // 篮球
            api = "basketball/list"
        case 35: // 足球
            api = "football/list"
        case 5:  // 徒步
            api = "hiking/list"
        case 14: // 登山
            api = "mountaineering/list"
        case 11: // 骑行
            api = "ride/list"
        case 4:  // 跑步
            api = "run/list"
        case 20: // 跑步机
            api = "running-machine/list"
        default:
            Log.error("Not support sport type: \(type)")
        }
        guard let api = api else { return nil }
        return POST(api, params, completion)
    }
}

extension APIManager {
    /// 查询睡眠的周、月数据，这里只查询 周、月数据
    @discardableResult
    func getSleepData(_ date: Date,
                      _ granularity: DateGranularity,
                      imei: String?,
                      completion: APICompletion<SleepNormalDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        switch granularity {
        case .week:
            return POST("sleep-monitor/weekly", params, completion)
        case .month:
            return POST("sleep-monitor/monthly", params, completion)
        case .day, .year:
            return nil
        }
    }
    
    /// 查询睡眠的天数据
    @discardableResult
    func getSleepDailyData(_ date: Date,
                            imei: String?,
                            completion: APICompletion<SleepDailyDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        return POST("sleep-monitor/daily", params, completion)
    }
    
    /// 查询睡眠的年数据
    @discardableResult
    func getSleepYearlyData(_ date: Date,
                            imei: String?,
                            completion: APICompletion<SleepYearlyDataModel>? = nil)
    -> APITASK? {
        let params = chartsCommonParams(date, imei: imei)
        return POST("sleep-monitor/yearly", params, completion)
    }
}
